
#import "JMCommonUtils.h"

#import <SystemConfiguration/SystemConfiguration.h>
#import <netinet/in.h>
#import <CommonCrypto/CommonDigest.h>
#import <objc/runtime.h>

#import "Definition.h"

@implementation JMCommonUtils

//判断手机是否联网状态
+ (BOOL)isNetworkReachable {
    //Create zero addy
    struct sockaddr_in zeroAddress;
    bzero(&zeroAddress, sizeof(zeroAddress));
    zeroAddress.sin_len = sizeof(zeroAddress);
    zeroAddress.sin_family = AF_INET;

    //Recover reachability flags
    SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
    SCNetworkReachabilityFlags flags;

    BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
    CFRelease(defaultRouteReachability);

    if (!didRetrieveFlags) {
        return NO;
    }

    BOOL isReachable = flags & kSCNetworkFlagsReachable;
    BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
    return (isReachable && !needsConnection) ? YES : NO;
}

#pragma mark -

- (void)dealloc {
}

+ (JMCommonUtils *)sharedInstance {
    static JMCommonUtils *Instance;
    @synchronized(Instance) {
        if (!Instance) {
            Instance = [[JMCommonUtils alloc] init];
        }
    }
    return Instance;
}

- (id)init {
    self = [super init];
    if (self) {
    }
    return self;
}

@end

//========================================

@interface NSArray (CFLog)
@end

//========================================

@interface NSDictionary (CFLog)
@end

//========================================

@implementation NSArray (CFLog)

- (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level {
    NSMutableString *nmString = [NSMutableString stringWithString:@"\n[\n"];
    [self enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) {
      [nmString appendFormat:@"\t%@\n", obj];
    }];
    [nmString appendString:@"]\n"];
    return nmString.copy;
}

@end

//========================================

@implementation NSDictionary (CFLog)

- (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level {
    NSMutableString *nmString = [NSMutableString stringWithString:@"\n{\n"];
    [self enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL *_Nonnull stop) {
      [nmString appendFormat:@"\t%@ = %@\n", key, obj];
    }];
    [nmString appendString:@"\n}\n"];
    return nmString.copy;
}

@end

//========================================

@implementation NSString (JSONCategories)

- (id)objectFromJSONString {
    if (self == nil) {
        return nil;
    }
    NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error = nil;
    id result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    //id result = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    if (error) {
        NSLog(@"%@", error);
        return nil;
    }
    return result;
}

@end

//========================================

@implementation NSObject (JSONCategories)

- (NSString *)JSONString {
    NSString *result = [[NSString alloc] initWithData:[self JSONData] encoding:NSUTF8StringEncoding];
    return result;
}

- (NSData *)JSONData {
    if (self == nil) {
        return nil;
    }
    NSError *error = nil;
    NSData *result = [NSJSONSerialization dataWithJSONObject:self options:kNilOptions error:&error];
    //NSData *result = [NSKeyedArchiver archivedDataWithRootObject:self];
    if (error) {
        NSLog(@"%@", error);
        return nil;
    }
    return result;
}

- (id)objectFromJSONData {
    if (self == nil || ![self isKindOfClass:NSData.class]) {
        return nil;
    }
    NSError *error = nil;
    id result = [NSJSONSerialization JSONObjectWithData:(NSData *)self options:kNilOptions error:&error];
    //id result = [NSKeyedUnarchiver unarchiveObjectWithData:(NSData *)self];
    if (error) {
        NSLog(@"%@", error);
        return nil;
    }
    return result;
}

@end

//========================================

@implementation UIAlertController (pruneNegative)

- (void)pruneNegativeWidthViewDidLoad {
    [super viewDidLoad];
    [self pruneNegativeWidthConstraints];
}

+ (void)load {
    method_exchangeImplementations(class_getInstanceMethod(self, @selector(viewDidLoad)), class_getInstanceMethod(self, @selector(pruneNegativeWidthViewDidLoad)));
}

- (void)pruneNegativeWidthConstraints {
    for (UIView *subView in self.view.subviews) {
        for (NSLayoutConstraint *constraint in subView.constraints) {
            if ([constraint.debugDescription containsString:@"width == -16"]) {
                [subView removeConstraint:constraint];
            }
        }
    }
}

@end
